home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / java / threads / example-1.1 / PhilosopherArea.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  4.1 KB  |  137 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.awt.*;
  18. import java.awt.event.MouseAdapter;
  19. import java.awt.event.MouseEvent;
  20.  
  21. class PhilosopherArea extends Canvas {
  22.     PhilAnimator1_1 controller;
  23.  
  24.     static final double MARGIN = 10.0f;
  25.     static final int NUMPHILS = 5;
  26.  
  27.     Image[] imgs = new Image[3];
  28.  
  29.     double spacing;
  30.  
  31.     Philosopher[] philosophers = new Philosopher[NUMPHILS];
  32.     Chopstick[] chopsticks = new Chopstick[NUMPHILS];
  33.     boolean[] redraw = new boolean[NUMPHILS];
  34.     String[] names = { "Arisduktle" , "Dukrates", "Pythagorduke", "Duko", "Dukimedes" };
  35.  
  36.     boolean running = false;
  37.  
  38.     public PhilosopherArea(PhilAnimator1_1 controller) {
  39.         super();
  40.         this.controller = controller;
  41.  
  42.         MediaTracker mt;
  43.         mt = new MediaTracker(this);
  44.  
  45.         imgs[0] = controller.getImage(controller.getCodeBase(), "hungryduke.gif");
  46.         mt.addImage(imgs[0], 0);
  47.         imgs[1] = controller.getImage(controller.getCodeBase(), "rightspoonduke.gif");
  48.         mt.addImage(imgs[1], 1);
  49.         imgs[2] = controller.getImage(controller.getCodeBase(), "bothspoonsduke.gif");
  50.         mt.addImage(imgs[2], 2);
  51.  
  52.         try {
  53.             mt.waitForID(0);
  54.             mt.waitForID(1);
  55.             mt.waitForID(2);
  56.         } catch (java.lang.InterruptedException e) {
  57.             System.out.println("Couldn't load one of the images");
  58.         }
  59.  
  60.         spacing = imgs[0].getWidth(this) + MARGIN;
  61.         createPhilosophersAndChopsticks();
  62.         addMouseListener(new MyAdapter());
  63.     }
  64.  
  65.     public void paint(Graphics g) {
  66.         g.setColor(Color.lightGray);
  67.         g.fillRect(0, 0, getSize().width, getSize().height);
  68.         for (int i = 0; i < NUMPHILS; i++) {
  69.             redraw[i] = true;
  70.         }
  71.         update(g);
  72.     }
  73.  
  74.     public void update(Graphics g) {
  75.         for (int i = 0; i < NUMPHILS; i++) {
  76.             if (redraw[i]) {
  77.                 philosophers[i].paint(g);
  78.                 redraw[i] = false;
  79.             }
  80.         }
  81.     }
  82.  
  83.     class MyAdapter extends MouseAdapter {
  84.         public void mouseClicked(MouseEvent evt) {
  85.             if (running) {
  86.                 for (int i = 0; i < NUMPHILS; i++)
  87.                     philosophers[i].suspend();
  88.             } else {
  89.                 for (int i = 0; i < NUMPHILS; i++)
  90.                     philosophers[i].resume();
  91.             }
  92.  
  93.             running = !running;
  94.     }
  95.     }
  96.  
  97.     public synchronized void repaintPhil(int id) {
  98.         redraw[id] = true;
  99.         repaint();
  100.     }
  101.  
  102.     public void startPhilosophers() {
  103.         for (int i = 0; i < NUMPHILS; i++)
  104.             philosophers[i].start();
  105.         running = true;
  106.     }
  107.  
  108.     public void stopPhilosophers() {
  109.         for (int i = 0; i < NUMPHILS; i++)
  110.             philosophers[i].stopRequested();
  111.     }
  112.  
  113.     public void createPhilosophersAndChopsticks() {
  114.         double x, y;
  115.         double radius = 80.0;
  116.         double centerAdj = 85.0;
  117.         double radians;
  118.  
  119. /* for a straight line
  120.         y = MARGIN;
  121. */
  122.         for (int i = 0; i < NUMPHILS; i++)
  123.             chopsticks[i] = new Chopstick();
  124.  
  125.         for (int i = 0; i < NUMPHILS; i++) {
  126. /* for a straight line
  127.             x = i * spacing;
  128. */
  129.             radians = i*(2.0 * Math.PI /(double)NUMPHILS);
  130.             x = Math.sin(radians) * radius + centerAdj; 
  131.             y = Math.cos(radians) * radius + centerAdj; 
  132.             philosophers[i] = new Philosopher(this, x, y, i);
  133.             repaintPhil(i);   
  134.         }
  135.     }
  136. }
  137.